package com.pasche;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Servlet implementation class buscarMobile
 */
@WebServlet("/buscarMobile")
public class buscarMobile extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public buscarMobile() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String dir = null;
		Double lat = null;
		Double lon = null;
		Integer serv = null;
		Conversor conv = new Conversor();
		if (request.getParameterMap().containsKey("dir")){
		 dir = request.getParameter("dir");
		}
		if (request.getParameterMap().containsKey("lat") && request.getParameterMap().containsKey("lon")){
	      lat = Double.parseDouble(request.getParameter("lat"));
		  lon = Double.parseDouble(request.getParameter("lon"));
		}
		if (request.getParameterMap().containsKey("serv")){
		  serv = Integer.parseInt(request.getParameter("serv"));
		}
	    Buscar bus = new Buscar();
		Ubicacion[] cercanos = null;
		try {
			cercanos = bus.buscarCercanos(serv, dir, lat, lon);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONObject jsonFinal = new JSONObject();
		JSONArray json = new JSONArray();
		try {
		for (Ubicacion ub : cercanos) {
			try {
				JSONObject ubicacionJSON = new JSONObject();
				ubicacionJSON.put("latitud", ub.getLatitud());
				ubicacionJSON.put("longitud", ub.getLongitud());
				if(ub.getCalle() == null){
					String add = conv.convertToAdress(ub.getLatitud(), ub.getLongitud());
					ub.setCalle(add);
				}
				ubicacionJSON.put("direccion", ub.getCalle());
				ubicacionJSON.put("altura", ub.getAltura());
				json.put(ubicacionJSON);
				ubicacionJSON = null;
			} catch (JSONException e) {
				jsonFinal.put("data", "null");
				jsonFinal.put("error", "true");
				PrintWriter out = response.getWriter();
		        out.println(jsonFinal);
				return;
			}
		}
			jsonFinal.put("data", json);
			jsonFinal.put("error", "null");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		PrintWriter out = response.getWriter();
        out.println("jpCallback(["+jsonFinal+"])");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
